package com.hanhanxiaochong.zhongxiangchuanmei.view.order.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ProductEvaluationBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.DensityUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GifSizeFilter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.MyGlideEngine;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.StarBar;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.WrapLinearLayout;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.utils.FileProviderUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by RockPhoenix on 2018/7/5.
 */

public class EvaluationAdapter extends CommonAdapter<ProductEvaluationBean.StoreBean.ProductBean> {

    private static final int REQUEST_CODE_CHOOSE = 0x01;
    private String order_num;
    private int uploadPosition;
    //所有产品上传的评价图片url集合
    private List<List<String>> urls;
    //所有产品的产品id集合
    private List<String> productIdList = new ArrayList<>();
    //所有产品的门店id集合
    private List<String> shopIdList = new ArrayList<>();
    //所有产品的项目分集合
    private List<String> productScoreList = new ArrayList<>();
    //所有产品的门店分集合
    private List<String> shopScoreList = new ArrayList<>();
    //所有产品的服务分集合
    private List<String> serviceScoreList = new ArrayList<>();
    //所有产品的评价内容集合
    private List<String> evaluationContentList = new ArrayList<>();

    public EvaluationAdapter(Context context, int layoutId, List<ProductEvaluationBean.StoreBean.ProductBean> datas, String order_num) {
        super(context, layoutId, datas);
        this.order_num = order_num;
        urls = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            ProductEvaluationBean.StoreBean.ProductBean bean = datas.get(i);
            urls.add(new ArrayList<>());
            productIdList.add(bean.getP_id());
            shopIdList.add(bean.getS_id());
            productScoreList.add("0");
            shopScoreList.add("0");
            serviceScoreList.add("0");
            evaluationContentList.add("");
        }
    }

    @Override
    protected void convert(ViewHolder holder, ProductEvaluationBean.StoreBean.ProductBean productBean, int position) {
        ImageView ivGoodPic = holder.getView(R.id.iv_good_pic);
        ImageView ivSelfSupport = holder.getView(R.id.iv_self_support);
        TextView tvGoodName = holder.getView(R.id.tv_good_name);
        TextView tvVipPrice = holder.getView(R.id.tv_vip_price);
        TextView tvNormalPrice = holder.getView(R.id.tv_normal_price);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + productBean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
        tvGoodName.setText(productBean.getP_name());
        ivSelfSupport.setVisibility(productBean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
        tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), productBean.getPrice_vip()));
        tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), productBean.getPrice()));

        StarBar starProject = holder.getView(R.id.star_project);
        StarBar starService = holder.getView(R.id.star_service);
        StarBar starShop = holder.getView(R.id.star_shop);
        TextView scoreProject = holder.getView(R.id.tv_score_project);
        TextView scoreService = holder.getView(R.id.tv_score_service);
        TextView scoreShop = holder.getView(R.id.tv_score_shop);
        starProject.setIntegerMark(true);
        starService.setIntegerMark(true);
        starShop.setIntegerMark(true);
        starProject.setStarMark(Float.parseFloat(productScoreList.get(position)));
        starService.setStarMark(Float.parseFloat(serviceScoreList.get(position)));
        starShop.setStarMark(Float.parseFloat(shopScoreList.get(position)));
        scoreProject.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(productScoreList.get(position))));
        scoreService.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(serviceScoreList.get(position))));
        scoreShop.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(shopScoreList.get(position))));
        starProject.setOnStarChangeListener(mark -> {
            scoreProject.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(mark)));
            productScoreList.set(position, String.valueOf(mark));
        });
        starService.setOnStarChangeListener(mark -> {
            scoreService.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(mark)));
            serviceScoreList.set(position, String.valueOf(mark));
        });
        starShop.setOnStarChangeListener(mark -> {
            scoreShop.setText(String.format(ResourcesUtils.getString(R.string.score), String.valueOf(mark)));
            shopScoreList.set(position, String.valueOf(mark));
        });
        EditText evaluationContent = holder.getView(R.id.et_evaluation_content);
        evaluationContent.setText(evaluationContentList.get(position));
        evaluationContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                evaluationContentList.set(position, s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.setText(R.id.tv_shop_name, productBean.getS_id() == null ? "总店" : productBean.getS_name());
        holder.setText(R.id.tv_order_num, order_num);
        WrapLinearLayout picContainer = holder.getView(R.id.ll_evaluation_pic_container);
        for (String url : urls.get(position)) {
            View picView = ResourcesUtils.inflate(R.layout.cl_evaluation_pic);
            ImageView pic = picView.findViewById(R.id.iv_evaluation_pic);
            ImageView deletePic = picView.findViewById(R.id.iv_delete_pic);
            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + url, R.mipmap.img_error, R.mipmap.img_error, pic);
            deletePic.setOnClickListener(view -> {
                picContainer.removeView(picView);
                urls.get(position).remove(url);
            });
            picContainer.addView(picView);
            ViewGroup.LayoutParams layoutParams = picView.getLayoutParams();
            layoutParams.width = DensityUtil.dip2px(90, mContext);
            layoutParams.height = DensityUtil.dip2px(90, mContext);
            picView.requestLayout();
        }
        holder.setOnClickListener(R.id.iv_choose_pic, v -> {
            this.uploadPosition = position;
            Matisse.from((Activity) mContext)
                    .choose(MimeType.allOf())
                    .countable(true)
                    .maxSelectable(6)
                    .captureStrategy(new CaptureStrategy(true, FileProviderUtils.getFileProviderName(mContext)))
                    .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                    .gridExpectedSize(mContext.getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .imageEngine(new MyGlideEngine())
                    .forResult(REQUEST_CODE_CHOOSE);
        });
    }

    public void getUploadPic(List<String> data) {
        urls.get(uploadPosition).addAll(data);
        notifyItemChanged(uploadPosition);
    }

    public String getAllPicUrls() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < urls.size(); i++) {
            List<String> singleProductPicUrlList = urls.get(i);
            if (singleProductPicUrlList.size() > 0) {
                for (int j = 0; j < singleProductPicUrlList.size(); j++) {
                    if (j != singleProductPicUrlList.size() - 1) {
                        stringBuilder.append(singleProductPicUrlList.get(j)).append(",");
                    } else {
                        if (i != urls.size() - 1) {
                            stringBuilder.append(singleProductPicUrlList.get(j)).append("?");
                        } else {
                            stringBuilder.append(singleProductPicUrlList.get(j));
                        }
                    }
                }
            } else {
                stringBuilder.append("?");
            }

        }
        return stringBuilder.toString();
    }

    public String getProductIdList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < productIdList.size(); i++) {
            if (i != productIdList.size() - 1) {
                stringBuilder.append(productIdList.get(i)).append(",");
            } else {
                stringBuilder.append(productIdList.get(i));
            }
        }
        return stringBuilder.toString();
    }

    public String getShopIdList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < shopIdList.size(); i++) {
            if (i != shopIdList.size() - 1) {
                stringBuilder.append(shopIdList.get(i)).append(",");
            } else {
                stringBuilder.append(shopIdList.get(i));
            }
        }
        return stringBuilder.toString();
    }

    public String getProductScoreList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < productScoreList.size(); i++) {
            String score = productScoreList.get(i);
            if (score.equals("")) {
                //未打分视作默认好评
                score = "5.0";
            }
            if (i != productScoreList.size() - 1) {
                stringBuilder.append(score).append(",");
            } else {
                stringBuilder.append(score);
            }
        }
        return stringBuilder.toString();
    }

    public String getShopScoreList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < shopScoreList.size(); i++) {
            String score = shopScoreList.get(i);
            if (score.equals("")) {
                //未打分视作默认好评
                score = "5.0";
            }
            if (i != shopScoreList.size() - 1) {
                stringBuilder.append(shopScoreList.get(i)).append(",");
            } else {
                stringBuilder.append(shopScoreList.get(i));
            }
        }
        return stringBuilder.toString();
    }

    public String getServiceScoreList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < serviceScoreList.size(); i++) {
            String score = serviceScoreList.get(i);
            if (score.equals("")) {
                //未打分视作默认好评
                score = "5.0";
            }
            if (i != serviceScoreList.size() - 1) {
                stringBuilder.append(serviceScoreList.get(i)).append(",");
            } else {
                stringBuilder.append(serviceScoreList.get(i));
            }
        }
        return stringBuilder.toString();
    }

    public String getEvaluationContentList() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < evaluationContentList.size(); i++) {
            String content = evaluationContentList.get(i);
            if (content.equals("")) {
                //未输入评价内容视作默认好评
                content = "用户默认做出好评";
            }
            if (i != evaluationContentList.size() - 1) {
                stringBuilder.append(content).append(",");
            } else {
                stringBuilder.append(content);
            }
        }
        return stringBuilder.toString();
    }
}
